package com.aoyama.office.libreoffice.service;

//import com.aoyama.office.libreoffice.util.PdfUtils;
//import org.jodconverter.core.DocumentConverter;
//import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
//import org.jodconverter.core.document.DocumentFormat;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

@Service
public class AttachementService {

//    @Resource
//    private DocumentConverter converter;
//
//    public void libreOfficeOnlinePreview(String netFileUrl, HttpServletResponse response) throws Exception {
//        //获取文件类型
//        String[] str = netFileUrl.split("\\.");
//
//        if (str.length == 0) {
//            throw new Exception("文件格式不正确");
//        }
//        String suffix = str[str.length - 1];
//        if (!suffix.equals("txt") && !suffix.equals("doc") && !suffix.equals("docx") && !suffix.equals("xls")
//                && !suffix.equals("xlsx") && !suffix.equals("ppt") && !suffix.equals("pptx")) {
//            throw new Exception("文件格式不支持预览");
//        }
//
//        // 创建URL
//        URL url = new URL(netFileUrl);
//        // 试图连接并取得返回状态码
//        URLConnection urlconn = url.openConnection();
//        urlconn.connect();
//        HttpURLConnection httpconn = (HttpURLConnection) urlconn;
//        int httpResult = httpconn.getResponseCode();
//        if (httpResult == HttpURLConnection.HTTP_OK) {
//            InputStream inputStream = urlconn.getInputStream();
////            OutputStream outputStreamLocal = new BufferedOutputStream(response.getOutputStream());
//            File outputFile = new File("/Users/aoyama/logs/openoffice11.pdf");
//            DocumentFormat targetFormat = DefaultDocumentFormatRegistry.PDF;
//            converter.convert(inputStream).to(outputFile).as(targetFormat).execute();
//
//            // 清空response
//            response.reset();
//// 设置response的Header
//            response.setCharacterEncoding("UTF-8");
////Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
////attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
//// filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
//            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("test", "UTF-8"));
//// 告知浏览器文件的大小
////            response.addHeader("Content-Length", "" + file.length());
//            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
//            response.setContentType("application/octet-stream");
//
//            converter.convert(inputStream).to(outputStream).as(targetFormat).execute();
//            outputStream.flush();
//        }
//    }

}
